package com.tcfvic.client.panels.widgets.widgetcontents;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.tcfvic.client.PersonService;
import com.tcfvic.client.PersonServiceAsync;
import com.tcfvic.client.panels.widgets.SimpleMessagePopup;
import com.tcfvic.client.panels.widgets.TCFWidgetContent;
import com.tcfvic.client.pojos.Jsp;
import com.tcfvic.client.pojos.Person;
import com.tcfvic.client.pojos.Personjsp;
import com.tcfvic.client.pojos.PersonjspId;
import com.tcfvic.client.tools.Month;
import com.tcfvic.client.tools.Tools;

public class JSPTab extends AbstractDataEntryWidgets implements
		TCFWidgetContent {
	private ListBox jspList = new ListBox();
	private TextBox jspid = new TextBox();
	private TextBox consultantName = new TextBox();
	private TextBox consultantEmail = new TextBox();
	private TextBox consultantPhone = new TextBox();
	private TextBox consultantMobile = new TextBox();

	private TextBox managerName = new TextBox();
	private TextBox managerEmail = new TextBox();
	private TextBox managerPhone = new TextBox();
	private TextBox managerMobile = new TextBox();
	private ListBox dayReg = new ListBox();
	private ListBox monthReg = new ListBox();
	private ListBox yearReg = new ListBox();
	private ListBox regStream = new ListBox();
	private TextArea notes = new TextArea();
	private ListBox registered = new ListBox();

	private final PersonServiceAsync personService = GWT
			.create(PersonService.class);

	public JSPTab() {
		// initialise();
	}

	@Override
	public Widget getContent() {
		// Create a table to layout the form options
		FlexTable ft = new FlexTable();
		ft.setCellSpacing(4);
		ft.setWidth("100%");

		// Add some standard form options
		int row = 0;
		ft.setHTML(row, 0, "JSAP");
		ft.setWidget(row, 1, jspList);
		ft.setHTML(row, 2, "Job seeker ID");
		ft.setWidget(row++, 3, jspid);
		ft.setHTML(row, 0, "Consultant name");
		ft.setWidget(row, 1, consultantName);
		ft.setHTML(row, 2, "Consultant email");
		ft.setWidget(row++, 3, consultantEmail);
		ft.setHTML(row, 0, "Consultant phone");
		ft.setWidget(row, 1, consultantPhone);
		ft.setHTML(row, 2, "Consultant mobile");
		ft.setWidget(row++, 3, consultantMobile);
		ft.setHTML(row, 0, "Manager name");
		ft.setWidget(row, 1, managerName);
		ft.setHTML(row, 2, "Manager email");
		ft.setWidget(row++, 3, managerEmail);
		ft.setHTML(row, 0, "Manager phone");
		ft.setWidget(row, 1, managerPhone);
		ft.setHTML(row, 2, "Manager mobile");
		ft.setWidget(row++, 3, managerMobile);
		ft.setHTML(row, 0, "Registration stream");
		regStream = getRegStream();
		ft.setWidget(row, 1, regStream);
		ft.setHTML(row, 2, "Notes");
		ft.setWidget(row++, 3, notes);

		// DATE STARTED
		HorizontalPanel hp = new HorizontalPanel();
		dayReg = Tools.getDayListBox();
		monthReg = Tools.getMonthListBox();
		yearReg = Tools.getYearListBox(2009);
		hp.add(dayReg);
		hp.add(monthReg);
		hp.add(yearReg);
		HTML label = new HTML("Date of registration");
		ft.setWidget(row, 0, label);
		ft.setWidget(row, 1, hp);
		registered = Tools.getYesList();
		ft.setHTML(row, 2, "Did not register");
		ft.setWidget(row++, 3, registered);

		saveButton.getElement().setId("saveButton");
		ft.setWidget(row++, 0, saveButton);

		return ft;
	}

	public ListBox getRegStream() {
		ListBox lb = new ListBox();
		lb.addItem("", "");
		lb.addItem("1", "1");
		lb.addItem("2", "2");
		lb.addItem("3", "3");
		lb.setSelectedIndex(0);
		return lb;
	}

	@Override
	public void initialise() {
		jspList.clear();
		jspList.addItem("");
		personService.getAllJSPs(new AsyncCallback<List<Jsp>>() {
			@Override
			public void onSuccess(List<Jsp> result) {
				for (Jsp jsp : result) {
					jspList.addItem(jsp.getName() + " - " + jsp.getSuburb(),
							String.valueOf(jsp.getId()));
				}
			}

			@Override
			public void onFailure(Throwable caught) {
				GWT.log("Could not retreive the JSP list", caught);
				new SimpleMessagePopup("Could not retreive the JSP list: "
						+ caught, null).showPopup();
			}
		});

	}

	@SuppressWarnings("unchecked")
	@Override
	public <T> T saveContent(T t) throws Exception {
		if (jspList.getSelectedIndex() > 0) {
			if (t != null && t instanceof Person) {
				Person person = (Person) t;
				Personjsp personJsp = new Personjsp();
				personJsp
						.setId(new PersonjspId(person.getId(), Integer
								.valueOf(jspList.getValue(jspList
										.getSelectedIndex()))));
				personJsp.setJspid(jspid.getText());
				personJsp.setConsultantsname(consultantName.getText());
				personJsp.setConsultantsemail(consultantEmail.getText());
				personJsp.setConsultantsphone(consultantPhone.getText());
				personJsp.setConsultantmobile(consultantMobile.getText());

				personJsp.setManagersname(managerName.getText());
				personJsp.setManagersemail(managerEmail.getText());
				personJsp.setManagerphone(managerPhone.getText());
				personJsp.setManagermobile(managerMobile.getText());
				if (registered != null && registered.getSelectedIndex() >= 0) {
					personJsp.setRegistered(registered.getValue(registered
							.getSelectedIndex()));
				}
				personJsp.setNotes(notes.getText());
				if (regStream.getSelectedIndex() > 0) {
					personJsp.setRegistrationstream(regStream
							.getItemText(regStream.getSelectedIndex()));
				}
				if (Tools.isValidDate(dayReg, monthReg, yearReg)) {
					personJsp.setDateofregistration(Tools.parseDate(dayReg,
							monthReg, yearReg));
				}
				Set<Personjsp> personJspSet = new HashSet<Personjsp>();
				personJspSet.add(personJsp);
				person.setPersonjsps(personJspSet);
				t = (T) person;
			}
		}
		return t;
	}

	@Override
	public void updateContent(Object o) {
		clear();
		if (o instanceof Person) {
			Person person = (Person) o;
			if (person.getPersonjsps() != null) {
				for (Personjsp pj : person.getPersonjsps()) {
					jspList.setSelectedIndex(getListIndexBasedOnId(jspList,
							String.valueOf(pj.getJsp().getId())));
					jspid.setText(pj.getJspid());
					consultantName.setText(pj.getConsultantsname());
					consultantEmail.setText(pj.getConsultantsemail());
					consultantPhone.setText(pj.getConsultantsphone());
					consultantMobile.setText(pj.getConsultantmobile());
					managerName.setText(pj.getManagersname());
					managerEmail.setText(pj.getManagersemail());
					managerPhone.setText(pj.getManagerphone());
					managerMobile.setText(pj.getManagermobile());
					regStream.setSelectedIndex(Tools
							.getSelectedIndexBasedOnItem(regStream, pj
									.getRegistrationstream()));
					registered.setSelectedIndex(Tools.getSelectedIndex(
							registered, String.valueOf(pj.getRegistered())));
					notes.setText(pj.getNotes());
					if (pj.getDateofregistration() != null) {
						dayReg.setSelectedIndex(Tools
								.getSelectedIndexBasedOnItem(dayReg, Tools
										.getDay(pj.getDateofregistration())));
						monthReg.setSelectedIndex(Month.valueOf(
								Tools.getMonth(pj.getDateofregistration())
										.toUpperCase()).getId());
						yearReg.setSelectedIndex(Tools.getSelectedIndex(
								yearReg, Tools.getYear(pj
										.getDateofregistration())));
					}
					// there should only be 1 in the list.
					break;
				}
			}
		}

	}

	@Override
	public void clear() {
		jspid.setText("");
		consultantName.setText("");
		consultantEmail.setText("");
		consultantPhone.setText("");
		consultantMobile.setText("");
		managerName.setText("");
		managerEmail.setText("");
		managerPhone.setText("");
		managerMobile.setText("");
		notes.setText("");
		Tools.resetList(registered, jspList, dayReg, monthReg, yearReg,
				regStream);
	}

	@Override
	public String getName() {
		return "JSAP";
	}

	@Override
	public void createNewRecord() {
		// TODO Auto-generated method stub

	}

}
